package pl.world.actors.proto
{
	import pl.engine.view.View;
	import pl.engine.camera.Camera;
	import pl.engine.model.MapGrid;
	import pl.engine.model.vo.obstacle.BaseObstacle;
	import pl.error.InternalError;

	import flash.geom.Point;

	/**
	 * ...
	 * @author Bakudo
	 */
	public class BaseActor extends BaseObstacle implements IActor
	{
		private var _updateView:Boolean;
		
		
		/** tu trzymamy punkty po ktorych musi sie poruszac obiekt */
		protected var path : Vector.<Point> = new Vector.<Point>;
		
		/** predkosc z jaka obiekt przebywa dystans z punktu do punktu*/
		private var speed : Number;
		
		private var nodeCompletion : Number;
		
		public function BaseActor( _x:uint, _y:uint, _type:uint, atlasID:uint , speed:Number )
		{
			super( _x, _y, _type, atlasID );
			this.speed = speed;
		}

		override protected function toOverride():void
		{
			
		}

		public static function factory( _x:uint, _y:uint, _type:uint, _texture:uint ,speed:Number):IActor
		{
			_x = Math.floor( _x );
			_y = Math.floor( _y );
			return new BaseActor( _x, _y, _type, _texture ,speed);
		}

		protected function doUpdateView():void
		{
			if (isRendered == true)
			{
				_display.x = isoX;
				_display.y = isoY;
			}
		}

		public function processLogic():void
		{
			// advance move
			// ...
			if (path != null)
			{
				if (path.length > 1)
				{
					if (nodeCompletion == 0 && isRendered == true) viewFaceingAngleChanged();
					var d : Number = Point.distance(path[0], path[1]);
					var piece : Number = d / speed;
					var interpolationOffset : Number = 1 / piece;
					nodeCompletion += interpolationOffset;
					if (nodeCompletion >= 1) nodeCompletion = 1;
					var interpolation : Point = Point.interpolate(path[1], path[0],nodeCompletion);
					x = interpolation.x;
					y = interpolation.y;
					
					if (nodeCompletion >= 1) {
						if (path.length == 2)
						{
							path.splice(0, 1);
							path.splice(0, 1);
							onAllPathFinished();
						}
						else
						{//trace('nastepny punkt');
							path.splice(0, 1);
						}
						nodeCompletion = 0;
					}
					if (Camera.isInViewRange(oX, oY) == true) updateView = true;
					//trace(interpolation, nodeCompletion);
					doUpdateView();
				}
			}
		}
		
		public function setMovePath(path:Vector.<Point>):void
		{
			nodeCompletion = 0;
			this.path = path;
			onPathIsUpdated();
		}
		
		public function set updateView( val:Boolean ):void
		{
			_updateView = val;
		}

		public function get updateView():Boolean
		{
			return _updateView;
		}

		override public function set x( val:uint ):void
		{
			_x = Math.floor(val);
			_oX = Math.floor( _x / MapGrid.TILE_W );
			var isop:Point = Camera.convertRectToIso( _x, _y );
			_isoX = isop.x - pivotX;
			_isoY = isop.y - pivotY;
			if (isRendered == true) updateView = true;
		}

		override public function set y( val:uint ):void
		{	_y = Math.floor(val);
			_oY = Math.floor( _y / MapGrid.TILE_H );
			var isop:Point = Camera.convertRectToIso( _x, _y );
			_isoX = isop.x - pivotX;
			_isoY = isop.y - pivotY;
			if (isRendered == true)
			{
				updateView = true;
				View.obstaclesNeedSorting = true;
			}
		}
	
		override public function get oX():uint
		{
			return _oX;
		}

		override public function get oY():uint
		{
			return _oY;
		}

		override public function get isoX():int
		{
			return _isoX;
		}

		override public function get isoY():int
		{
			return _isoY;
		}
		
		
		
		protected function viewFaceingAngleChanged():void
		{
			
		}
		
		protected function onAllPathFinished():void
		{
			
		}
		
		protected function onPathIsUpdated():void
		{
			
		}
	}
}